package com.thirteen.app.service.impl;

import com.thirteen.app.dao.AnswerDao;
import com.thirteen.app.dao.QuestionDao;
import com.thirteen.app.dao.QuestionTypeQuestionDao;
import com.thirteen.app.dto.AnswerQuestionDto;
import com.thirteen.app.dto.QuestionAnswerDto;
import com.thirteen.app.dto.WrongQuestions;
import com.thirteen.app.entity.Question;
import com.thirteen.app.entity.QuestionTypeQuestion;
import com.thirteen.app.service.QuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 题目(Question)表服务实现类
 *
 * @author 郑清林
 * @since 2021-09-06 10:29:42
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionDao questionDao;

    @Resource
    private AnswerDao answerDao;

    @Resource
    private QuestionTypeQuestionDao questionTypeQuestionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param questionid 主键
     * @return 实例对象
     */
    @Override
    public Question queryById(Integer questionid) {
        return this.questionDao.queryById(questionid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Question> queryAllByLimit(int offset, int limit) {
        return this.questionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public Question insert(Question question, List<AnswerQuestionDto> answers) {
        question.setQuestionId(questionDao.insert(question));//向题目表里面插入数据
        answerDao.insert(question.getQuestionId(),answers);//向该题目对应的答案表里面插入数据
        QuestionTypeQuestion newquestionTypeQuestion = new QuestionTypeQuestion();
        newquestionTypeQuestion.setQuestionId(question.getQuestionId());
        newquestionTypeQuestion.setQuestionType(question.getQuestionType());
        int type = question.getQuestionType();
        questionTypeQuestionDao.insert(newquestionTypeQuestion);//向题目类型和题目对应关系表插入数据
        return question;
    }

    /**
     * 修改数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public Question update(Question question) {
        this.questionDao.update(question);
        return this.queryById(question.getQuestionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param questionid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer questionid) {
        return this.questionDao.deleteById(questionid) > 0;
    }

    protected void handle(QuestionAnswerDto questionAnswerDto) {
        if (questionAnswerDto != null) {
            if (questionAnswerDto.getQuestionType() == 1) {   //处理单选题
                int rightAnswerIndex = 0;
                for (int i = 0; i < questionAnswerDto.getChoicesList().toArray().length; i++) {
                    if (questionAnswerDto.getChoicesList().get(i).getIsRight()) {
                        rightAnswerIndex = i;
                    }
                }
                questionAnswerDto.setSolution(String.valueOf(rightAnswerIndex));

            } else if (questionAnswerDto.getQuestionType() == 2) {    //处理多选题

                String s = "";
                int i = 0;
                for (i = 0; i < questionAnswerDto.getChoicesList().toArray().length; i++) {
                    if (questionAnswerDto.getChoicesList().get(i).getIsRight()) {

                        s += i + ",";
                    }
                }

                questionAnswerDto.setSolution(s.substring(0, s.length() - 1));
                System.out.println(questionAnswerDto.getSolution());
            } else if (questionAnswerDto.getQuestionType() == 3) {    //处理判断题
                if (questionAnswerDto.getChoicesList().get(0).getIsRight() == true) {
                    questionAnswerDto.setSolution("0");
                } else if (questionAnswerDto.getChoicesList().get(1).getIsRight() == true) {
                    questionAnswerDto.setSolution("1");
                }
            } else if (questionAnswerDto.getQuestionType() == 4 || questionAnswerDto.getQuestionType() == 5) {     //处理填空题和简答题
                questionAnswerDto.setSolution(questionAnswerDto.getChoicesList().get(0).getContent());
            }
        }
    }

    @Override
    public Map<String, Object> getquestionPageList(Integer pageIndex, Integer pageSize, Integer questionType, Integer repositoryId, Integer questionId) {
        Map<String, Object> map = new HashMap<>();
        QuestionAnswerDto questionAnswerDto = questionDao.getquestionPageList(pageIndex, pageSize, questionType, repositoryId, questionId).get(0);
        this.handle(questionAnswerDto);
        map.put("data", questionAnswerDto);
        return map;
    }

    @Override
    public int questionCount(Integer questionType, Integer repositoryId, Integer questionId) {
        return questionDao.questionCount(questionType, repositoryId, questionId);
    }

    @Override
    public int deleteQuestion(Integer questionId, Integer repositoryId) {
        return questionDao.deleteQuestion(questionId, repositoryId);
    }

    @Override
    public List<QuestionAnswerDto> getQuestionList(Integer id, Integer mode, Integer[] type, Integer userId) {
        if (mode == null) {
            List<QuestionAnswerDto> list = questionDao.getQuestionList(id, mode, null, userId);
            System.out.println(list);
            for (QuestionAnswerDto qa : list) {
                this.handle(qa);
            }
            return list;
        } else {
            if (mode == 0 || mode == 3) {
                List<QuestionAnswerDto> list = questionDao.getQuestionList(id, mode, type, userId);
                for (QuestionAnswerDto qa : list) {
                    this.handle(qa);
                }
                return list;
            } else if (mode == 1) {
                List<QuestionAnswerDto> list = questionDao.getQuestionList(id, mode, type, userId);
                for (QuestionAnswerDto qa : list) {
                    this.handle(qa);
                }
                Random random = new Random(4);
                Collections.shuffle(list, random);
                return list;
            } else {
                List<QuestionAnswerDto> list = questionDao.getQuestionListMode_2(id, mode, type, userId);
                for (QuestionAnswerDto qa : list) {
                    this.handle(qa);
                }
                return list;
            }
        }


    }

    @Override
    public List<WrongQuestions> selectAllWrongQuestions(Integer examRecordId,String content) {
        return questionDao.selectAllWrongQuestions(examRecordId,content);
    }

    @Override
    public int deleteWrongQu(Integer questionId, Integer examRecordId) {
        return questionDao.deleteWrongQu(questionId, examRecordId);
    }


}
